<template>
    <div class="container-content">
        <el-form
            ref="elForm"
            :model="formData"
            :rules="rules"
            size="medium"
            label-width="100px"
        >
            <!-- <el-form-item label-width="200px" label="编码" prop="costCode">
                <el-input
                    v-model="formData.costCode"
                    placeholder="请输入编码"
                    clearable
                    :style="{ width: '100%' }"
                >
                </el-input>
            </el-form-item> -->
            <el-form-item label-width="200px" label="部门属性" prop="deptAttr">
                <el-input
                    v-model="formData.deptAttr"
                    placeholder="请输入部门属性"
                    clearable
                    :style="{ width: '100%' }"
                >
                </el-input>
            </el-form-item>
            <el-form-item
                label-width="200px"
                label="关联部门"
                prop="relationId"
            >
                <el-select
                    v-model="formData.relation"
                    placeholder="请选择关联部门"
                    clearable
                    :style="{ width: '100%' }"
                >
                    <el-option
                        v-for="(item, index) in relationIdOptions"
                        :key="index"
                        :label="item.label"
                        :value="item.value"
                        :disabled="item.disabled"
                    ></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label-width="200px" label="名称" prop="title">
                <el-input
                    v-model="formData.title"
                    placeholder="请输入名称"
                    clearable
                    :style="{ width: '100%' }"
                ></el-input>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import { add } from "@/api/base/data";
import { updataKingBaseCostCenterList,editKingBaseCostCenterList } from "@/api/bim/outher";
export default {
    components: {},
     props: ["id"],
    data() {
        return {
            flag:false,
            formData: {
                costCode: undefined,
                deptAttr: undefined,
                relation: undefined,
                title: undefined,
            },
            rules: {
                costCode: [
                    {
                        required: true,
                        message: "请输入编码",
                        trigger: "blur",
                    },
                ],
                deptAttr: [
                    {
                        required: true,
                        message: "请输入部门属性",
                        trigger: "blur",
                    },
                ],
                relationId: [
                    {
                        required: true,
                        message: "请选择关联部门",
                        trigger: "change",
                    },
                ],
                title: [
                    {
                        required: true,
                        message: "请输入名称",
                        trigger: "blur",
                    },
                ],
            },
            relationIdOptions: [
                {
                    label: "创建",
                    value: 1,
                },
                {
                    label: "审核中",
                    value: 2,
                },
                {
                    label: "已审核",
                    value: 3,
                },
            ],
        };
    },
    computed: {},
    watch: {},
    created() {},
    mounted() {
        if (this.id!='') {
            updataKingBaseCostCenterList(this.id).then((res)=>{
                this.formData=res.data
                this.flag=true
            })
        }
    },
    methods: {
        submitForm(cb) {
            console.log(this.flag);
            
           if (this.flag) {
            console.log(789);
            
             this.$refs["elForm"].validate((valid) => {
                // if (!valid) return;
                // TODO 提交表单
                editKingBaseCostCenterList(this.formData, "KingBaseCostCenter").then((res) => {
                    if (res.code === 200) {
                        cb();
                        this.$message.success("修改成功");
                        this.flag=false
                    }
                });
            });
           }else{
            console.log(456);
            
             this.$refs["elForm"].validate((valid) => {
                // if (!valid) return;
                // TODO 提交表单
                add(this.formData, "KingBaseCostCenter").then((res) => {
                    if (res.code === 200) {
                        cb();
                        this.$message.success("新增成功");
                    }
                });
            });
           }
        },
        resetForm() {
            this.$refs["elForm"].resetFields();
        },
    },
};
</script>
<style></style>
